<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js循环结构之for..in</title>
</head>
<body>
  <ul>
    <li>for..in可遍历数组或对象</li>
    <li>对数组而言key为数组项的索引</li>
    <li>对对象而言key为对象的属性名</li>
  </ul>
  <script>
    // 遍历数组
    const books = [
      { title: 'JS学习', author: 'Miracle He' },
      { title: 'ES学习', author: 'Tom Wang' },
      { title: 'TS学习', author: 'Jack Li' }
    ]
    document.write('<table width="100%" border="1">')
    document.write('<thead><tr><th>标题</th><th>作者</th></tr></thead>')
    for (const key in books) {
      document.write(`
        <tr>
          <td>${books[key].title}</td>
          <td>${books[key].author}</td>
        </tr>`
      )
    }
    document.write('</table>')
    // 遍历对象
    const user = { name: 'Miracle', age: 35, skills: 'js,ts,es' }
    for (const key in user) {
      console.log(`${key}: ${user[key]}`)
    }
  </script>
</body>
</html>
